package com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.service.impl;

import com.css.fxfzxczh.constants.YNEnum;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.entity.OtherLoss;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.entity.ReportPublicField;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.repository.OtherLossRepository;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.repository.ReportPublicFieldRepository;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.strategy.StrategyService;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import org.apache.poi.hssf.usermodel.HSSFDataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/02/17
 * @Description: 其他类地震损失报表
 */
//@Service("其他类地震损失报表")
@Service("18")
public class OtherLossServiceImpl implements StrategyService {
    @Resource
    OtherLossRepository otherLossRepository;
    @Resource
    ReportPublicFieldRepository reportPublicFieldRepository;
    @Override
    public void reportImport(Workbook workbook, String xlrmId, String previousXlrmId, ReportPublicField reportPublicField) {
        if(!PlatformObjectUtils.isEmpty(previousXlrmId)){
            otherLossRepository.deleteByXlrmId(previousXlrmId);
            //删除报表公共字段
            reportPublicFieldRepository.deleteByXlrmId(previousXlrmId);
        }
        List<OtherLoss> list = getList(workbook,xlrmId);
        otherLossRepository.batchSave(list);
        reportPublicField.setProvince(list.get(0).getProvince());
        reportPublicField.setCity(list.get(0).getCity());
        reportPublicField.setCounty(list.get(0).getCounty());
        reportPublicField.setTownship(list.get(0).getTownship());
        //默认删除
        reportPublicField.setDelFlag(YNEnum.Y.toString());
        reportPublicFieldRepository.save(reportPublicField);
    }

    private List<OtherLoss> getList(Workbook workbook, String xlrmId){
        List<OtherLoss> dataLst = new ArrayList<>();
        if (workbook != null) {
            //循环除了第一行的所有行
            /** 得到第二个sheet */
            Sheet sheet = workbook.getSheetAt(0);
            /** 得到Excel的行数 */
            int totalRows = sheet.getPhysicalNumberOfRows();
            HSSFDataFormatter hSSFDataFormatter = new HSSFDataFormatter();
            //填表人
            String fillFormUser = hSSFDataFormatter.formatCellValue(sheet.getRow(1).getCell(1));
            //联系电话
            String tel = hSSFDataFormatter.formatCellValue(sheet.getRow(1).getCell(3));
            //填表日期
            String fillFormTime = hSSFDataFormatter.formatCellValue(sheet.getRow(1).getCell(5));
            //省
            String province = hSSFDataFormatter.formatCellValue(sheet.getRow(2).getCell(1));
            //市
            String city = hSSFDataFormatter.formatCellValue(sheet.getRow(2).getCell(3));
            //县
            String district = hSSFDataFormatter.formatCellValue(sheet.getRow(2).getCell(5));
            //乡
            String township = hSSFDataFormatter.formatCellValue(sheet.getRow(2).getCell(7));
            //村
            String village = hSSFDataFormatter.formatCellValue(sheet.getRow(2).getCell(9));
            /** 循环Excel的行 从第七行开始*/
            for (int r = 4; r < totalRows; r++) {
                Row row = sheet.getRow(r);
                if (row == null) {
                    continue;
                }
                OtherLoss obj = new OtherLoss();
                //主键id
                obj.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                //报表主表id
                obj.setXlrmId(xlrmId);
                //填表人
                obj.setFillFormUser(fillFormUser);
                //tel
                obj.setTel(tel);
                //填报日期
                obj.setFillFormTime(fillFormTime);
                //省
                obj.setProvince(province);
                //市
                obj.setCity(city);
                //县
                obj.setCounty(district);
                //乡
                obj.setTownship(township);
                //村
                obj.setVillage(village);
                //序列
                obj.setSequence(hSSFDataFormatter.formatCellValue(row.getCell(0)));
                //项目类别
                obj.setItemType(hSSFDataFormatter.formatCellValue(row.getCell(1)));
                //计量单位
                obj.setUnitMeasurement(hSSFDataFormatter.formatCellValue(row.getCell(2)));
                //单价(元)
                obj.setPrice(hSSFDataFormatter.formatCellValue(row.getCell(3)));
                //数量
                obj.setQuantity(hSSFDataFormatter.formatCellValue(row.getCell(4)));
                //损失(元)
                obj.setLoss(hSSFDataFormatter.formatCellValue(row.getCell(5)));
                //备注
                obj.setComment(hSSFDataFormatter.formatCellValue(row.getCell(6)));
                dataLst.add(obj);
            }
        }
        return dataLst;
    }
}
